package com.population.content;

import java.io.IOException;

import com.example.logindesign.Mainmenu;
import com.example.logindesign.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View.OnClickListener;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.spinner.keyvalue.SpinnerClass;

public class PopulationListViewActivity extends Activity {
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String ENDPOINT = "http://192.168.1.198:1536/CommunitySystemsicWsWF.asmx";
	
	private String methodName, soapAcion;
	private String spinnerLocation;
	
	// 为了初始化下拉列表用的几个Unitcode
	private String shiUnitcode, xianUnitcode, jiedaoUnitcode,
			shequUnitcode;
	
	// listview arrayList
	private ArrayList<HashMap<String, Object>> listItemArrayList;
	// 2个姓名edittext框，姓名，身份证
	private EditText nameEditText;
	private EditText idNumbereEditText;
	// 10个下拉列表
	private Spinner shiSpinner;
	private Spinner xianSpinner;
	private Spinner jiedaoSpinner;
	private Spinner shequSpinner;
	private Spinner xiaoquSpinner;
	private Spinner louSpinner;
	private Spinner danyuanSpinner;
	private Spinner cengSpinner;
	private Spinner huhaoSpinner;
	private ArrayList<SpinnerClass> shiSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> shiSpinnerAdapter;
	private ArrayList<SpinnerClass> xianSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> xianSpinnerAdapter;
	private ArrayList<SpinnerClass> jiedaoSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> jiedaoSpinnerAdapter;
	private ArrayList<SpinnerClass> shequSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> shequSpinnerAdapter;
	private ArrayList<SpinnerClass> xiaoquSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> xiaoquSpinnerAdapter;
	private ArrayList<SpinnerClass> louSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> louSpinnerAdapter;
	private ArrayList<SpinnerClass> danyuanSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> danyuanSpinnerAdapter;
	private ArrayList<SpinnerClass> cengSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> cengSpinnerAdapter;
	private ArrayList<SpinnerClass> huhaoSpinnerArrayList;
	private ArrayAdapter<SpinnerClass> huhaoSpinnerAdapter;
	// 搜索按钮
	private Button btnSearch;
	// 返回按钮
	private ImageButton btnBack;
	// 上拉刷新的控件
	private PullToRefreshListView mPullRefreshListView;
	private SimpleAdapter simpleAdapter;
	// 下边页数显示相关控件
	private TextView totalPageTextView;
	private TextView pageTextView;
	private TextView recordsTextView;
	// 总条数
	private int records = 0;
	// 总页数
	private int totalPage = 0;
	// 显示的条数
	private int rows = 10;
	// 当前页数
	private int page = 1;
	// unitcode
	private String unitcode = "";
	// webservice 参数
	private String xiaoqu, lou, louunit, ceng, huhao, xingbie, name, idNumber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_population_listview);
		
		// 取当前unitcode的上级和本级unitcode
		getMyUnitcodeAbout();
		// sharedpreference中取得相应值
		getValueFromSharedPreferences();
		// 绑定TextView,返回按钮等控件
		initTextViewEtc();
		// 绑定listview控件
		initListView();
	}

	/**
	 * sharedpreference中取得相应值
	 */
	private void getValueFromSharedPreferences() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences = getSharedPreferences("UserInfo",
				Activity.MODE_PRIVATE);
		unitcode = sharedPreferences.getString("unitcode", "");
	}

	/**
	 * 绑定TextView等控件
	 */
	private void initTextViewEtc() {
		// 得到控件
		totalPageTextView = (TextView) findViewById(R.id.txt_population_totalPage);
		pageTextView = (TextView) findViewById(R.id.txt_population_page);
		recordsTextView = (TextView) findViewById(R.id.txt_population_records);
		btnBack = (ImageButton) findViewById(R.id.btn_population_list_back);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 跳转到menu页面
				skipMainMenuActivity();
			}
		});
		btnSearch = (Button) findViewById(R.id.btn_population_search);
		btnSearch.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 搜索页面
				AlertDialog.Builder builder = new AlertDialog.Builder(
						PopulationListViewActivity.this);
				View view = View.inflate(
						PopulationListViewActivity.this,
						R.layout.alertdialog_population_searchdialog, null);

				// findviewbyid
				final TableRow xianTableRow = (TableRow) view
						.findViewById(R.id.tablerow_xian);
				final TableRow jiedaoTableRow = (TableRow) view
						.findViewById(R.id.tablerow_jiedao);
				final TableRow shequTableRow = (TableRow) view
						.findViewById(R.id.tablerow_shequ);
				final TableRow xiaoquTableRow = (TableRow) view
						.findViewById(R.id.tablerow_xiaoqu);
				final TableRow louTableRow = (TableRow) view
						.findViewById(R.id.tablerow_lou);
				final TableRow danyuanTableRow = (TableRow) view
						.findViewById(R.id.tablerow_danyuan);
				final TableRow cengTableRow = (TableRow) view
						.findViewById(R.id.tablerow_ceng);
				final TableRow huhaoTableRow = (TableRow) view
						.findViewById(R.id.tablerow_huhao);

				xianTableRow.setVisibility(View.GONE);
				jiedaoTableRow.setVisibility(View.GONE);
				shequTableRow.setVisibility(View.GONE);
				xiaoquTableRow.setVisibility(View.GONE);
				louTableRow.setVisibility(View.GONE);
				danyuanTableRow.setVisibility(View.GONE);
				cengTableRow.setVisibility(View.GONE);
				huhaoTableRow.setVisibility(View.GONE);

				nameEditText = (EditText) view
						.findViewById(R.id.population_edittext_name);
				idNumbereEditText = (EditText) view
						.findViewById(R.id.population_edittext_idNumber);
				shiSpinner = (Spinner) view
						.findViewById(R.id.population_shiSpinner);
				xianSpinner = (Spinner) view
						.findViewById(R.id.population_xianSpinner);
				jiedaoSpinner = (Spinner) view
						.findViewById(R.id.population_jiedaoSpinner);
				shequSpinner = (Spinner) view
						.findViewById(R.id.population_shequSpinner);
				xiaoquSpinner = (Spinner) view
						.findViewById(R.id.population_xiaoquSpinner);
				louSpinner = (Spinner) view
						.findViewById(R.id.population_louSpinner);
				danyuanSpinner = (Spinner) view
						.findViewById(R.id.population_danyuanSpinner);
				cengSpinner = (Spinner) view
						.findViewById(R.id.population_cengSpinner);
				huhaoSpinner = (Spinner) view
						.findViewById(R.id.population_huhaoSpinner);

				// setadapter
				shiSpinnerArrayList = new ArrayList<SpinnerClass>();
				shiSpinnerArrayList.add(new SpinnerClass("潍坊市", "3707000000"));
				shiSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						shiSpinnerArrayList);
				shiSpinner.setAdapter(shiSpinnerAdapter);
				shiSpinnerAdapter.notifyDataSetChanged();

				xianSpinnerArrayList = new ArrayList<SpinnerClass>();
				xianSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				xianSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						xianSpinnerArrayList);
				xianSpinner.setAdapter(xianSpinnerAdapter);
				xianSpinnerAdapter.notifyDataSetChanged();
				
				jiedaoSpinnerArrayList = new ArrayList<SpinnerClass>();
				jiedaoSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				jiedaoSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						jiedaoSpinnerArrayList);
				jiedaoSpinner.setAdapter(jiedaoSpinnerAdapter);
				jiedaoSpinnerAdapter.notifyDataSetChanged();

				shequSpinnerArrayList = new ArrayList<SpinnerClass>();
				shequSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				shequSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						shequSpinnerArrayList);
				shequSpinner.setAdapter(shequSpinnerAdapter);
				shequSpinnerAdapter.notifyDataSetChanged();

				xiaoquSpinnerArrayList = new ArrayList<SpinnerClass>();
				xiaoquSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				xiaoquSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						xiaoquSpinnerArrayList);
				xiaoquSpinner.setAdapter(xiaoquSpinnerAdapter);
				xiaoquSpinnerAdapter.notifyDataSetChanged();

				louSpinnerArrayList = new ArrayList<SpinnerClass>();
				louSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				louSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						louSpinnerArrayList);
				louSpinner.setAdapter(louSpinnerAdapter);
				louSpinnerAdapter.notifyDataSetChanged();
				
				danyuanSpinnerArrayList = new ArrayList<SpinnerClass>();
				danyuanSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				danyuanSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						danyuanSpinnerArrayList);
				danyuanSpinner.setAdapter(danyuanSpinnerAdapter);
				danyuanSpinnerAdapter.notifyDataSetChanged();

				cengSpinnerArrayList = new ArrayList<SpinnerClass>();
				cengSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				cengSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						cengSpinnerArrayList);
				cengSpinner.setAdapter(cengSpinnerAdapter);
				cengSpinnerAdapter.notifyDataSetChanged();
				
				huhaoSpinnerArrayList = new ArrayList<SpinnerClass>();
				huhaoSpinnerArrayList.add(new SpinnerClass("请选择", ""));
				huhaoSpinnerAdapter = new ArrayAdapter<SpinnerClass>(
						PopulationListViewActivity.this,
						android.R.layout.simple_spinner_dropdown_item,
						huhaoSpinnerArrayList);
				huhaoSpinner.setAdapter(huhaoSpinnerAdapter);
				huhaoSpinnerAdapter.notifyDataSetChanged();		
				
				huhaoSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						// TODO Auto-generated method stub
						String selectedValue = ((SpinnerClass) huhaoSpinner
								.getSelectedItem()).getValue();
						huhao = selectedValue;
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});
				
				cengSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						// TODO Auto-generated method stub
						String selectedValue = ((SpinnerClass) cengSpinner
								.getSelectedItem()).getValue();
						ceng = selectedValue;
						huhaoTableRow.setVisibility(View.GONE);
						spinnerLocation = "ceng";
						if (spinnerLocation.equals("ceng")) {
							huhaoTableRow.setVisibility(View.VISIBLE);
						} else {
							huhaoTableRow.setVisibility(View.GONE);
						}
						// 填充户号下拉列表数据
						new sethuhaoSpinnerTask().execute();
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});
				
				danyuanSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						// TODO Auto-generated method stub
						String selectedValue = ((SpinnerClass) danyuanSpinner
								.getSelectedItem()).getValue();
						louunit = selectedValue;
						cengTableRow.setVisibility(View.GONE);
						huhaoTableRow.setVisibility(View.GONE);
						spinnerLocation = "danyuan";
						if (spinnerLocation.equals("danyuan")) {
							cengTableRow.setVisibility(View.VISIBLE);
						} else {
							cengTableRow.setVisibility(View.GONE);
						}
						// 填充层下拉列表数据
						new setcengSpinnerTask().execute();
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});
				
				// setOnItemSelected
				louSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) louSpinner
										.getSelectedItem()).getValue();
								lou = selectedValue;
								spinnerLocation = "lou";
								if (spinnerLocation.equals("lou")) {
									danyuanTableRow.setVisibility(View.VISIBLE);
								} else {
									danyuanTableRow.setVisibility(View.GONE);
								}
								// 填充单元下拉列表数据
								new setunitSpinnerTask().execute();
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});
				xiaoquSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) xiaoquSpinner
										.getSelectedItem()).getValue();
								xiaoqu = selectedValue;
								danyuanTableRow.setVisibility(View.GONE);
								cengTableRow.setVisibility(View.GONE);
								huhaoTableRow.setVisibility(View.GONE);
								spinnerLocation = "xiaoqu";
								if (spinnerLocation.equals("xiaoqu")) {
									louTableRow.setVisibility(View.VISIBLE);
								} else {
									louTableRow.setVisibility(View.GONE);
								}
								// 填充楼下拉列表数据
								new setlouSpinnerTask().execute();
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				shequSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) shequSpinner
										.getSelectedItem()).getValue();
								unitcode = selectedValue;
								louTableRow.setVisibility(View.GONE);
								danyuanTableRow.setVisibility(View.GONE);
								cengTableRow.setVisibility(View.GONE);
								huhaoTableRow.setVisibility(View.GONE);
								
								spinnerLocation = "shequ";
								if (spinnerLocation.equals("shequ")) {
									xiaoquTableRow.setVisibility(View.VISIBLE);
								} else {
									xiaoquTableRow.setVisibility(View.GONE);
								}
								// 填充小区下拉列表数据
								new setxiaoquSpinnerTask().execute();
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				jiedaoSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) jiedaoSpinner
										.getSelectedItem()).getValue();
								unitcode = selectedValue;
								xiaoquTableRow.setVisibility(View.GONE);
								louTableRow.setVisibility(View.GONE);
								danyuanTableRow.setVisibility(View.GONE);
								cengTableRow.setVisibility(View.GONE);
								huhaoTableRow.setVisibility(View.GONE);
								spinnerLocation = "jiedao";
								if (spinnerLocation.equals("jiedao")) {
									shequTableRow.setVisibility(View.VISIBLE);
								} else {
									shequTableRow.setVisibility(View.GONE);
								}
								// 填充街道下拉列表数据
								new setshequSpinnerTask().execute();

							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				xianSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								String selectedValue = ((SpinnerClass) xianSpinner
										.getSelectedItem()).getValue();
								unitcode = selectedValue;
								shequTableRow.setVisibility(View.GONE);
								xiaoquTableRow.setVisibility(View.GONE);
								louTableRow.setVisibility(View.GONE);
								danyuanTableRow.setVisibility(View.GONE);
								cengTableRow.setVisibility(View.GONE);
								huhaoTableRow.setVisibility(View.GONE);
								spinnerLocation = "xian";
								if (spinnerLocation.equals("xian")) {
									// 选择县填充街道
									jiedaoTableRow.setVisibility(View.VISIBLE);
								} else {
									jiedaoTableRow.setVisibility(View.GONE);
								}
								// 填充街道下拉列表数据
								new setjiedaoSpinnerTask().execute();

							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

				shiSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> parent,
									View view, int position, long id) {
								// TODO Auto-generated method stub
								// 获取县级id
							    String a = unitcode.substring(0, 6);
							    String b = a+"0000";
							    unitcode = b;
								xianTableRow.setVisibility(View.VISIBLE);
								jiedaoTableRow.setVisibility(View.GONE);
								shequTableRow.setVisibility(View.GONE);
								xiaoquTableRow.setVisibility(View.GONE);
								louTableRow.setVisibility(View.GONE);
								danyuanTableRow.setVisibility(View.GONE);
								cengTableRow.setVisibility(View.GONE);
								huhaoTableRow.setVisibility(View.GONE);
								spinnerLocation = "shi";
								// 填充县下拉列表数据
								new setxianSpinnerTask().execute();
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});
				
				// 默认潍坊市选中
				shiSpinner.setSelection(0, true);
				shiSpinner.setEnabled(false);
				builder.setView(view);
				builder.setTitle("数据筛选");
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// 根据搜索条件查询
								String finallyUnitcodeshi = ((SpinnerClass)shiSpinner.getSelectedItem()).getValue();
								String finallyUnitcodexian = ((SpinnerClass)xianSpinner.getSelectedItem()).getValue();
								String finallyUnitcodejiedao = ((SpinnerClass)jiedaoSpinner.getSelectedItem()).getValue();
								String finallyUnitcodeshequ = ((SpinnerClass)shequSpinner.getSelectedItem()).getValue();
								String finallyUnitcodexiaoqu = ((SpinnerClass)xiaoquSpinner.getSelectedItem()).getValue();
								String finallyUnitcodelou = ((SpinnerClass)louSpinner.getSelectedItem()).getValue();
								String finallyUnitcodedanyuan = ((SpinnerClass)danyuanSpinner.getSelectedItem()).getValue();
								String finallyUnitcodeceng = ((SpinnerClass)cengSpinner.getSelectedItem()).getValue();
								String finallyUnitcodehuhao = ((SpinnerClass)huhaoSpinner.getSelectedItem()).getValue();
								
								if(!nameEditText.getText().toString().trim().equals(""))
								{
									name = nameEditText.getText().toString().trim();
								}
								
								if(!idNumbereEditText.getText().toString().trim().equals(""))
								{
									idNumber = idNumbereEditText.getText().toString().trim();
								}
								
								if(!finallyUnitcodeshi.equals("")){
									unitcode = finallyUnitcodeshi;
								}
								if(!finallyUnitcodexian.equals("")){
									unitcode = finallyUnitcodexian;
								}
								if(!finallyUnitcodejiedao.equals("")){
									unitcode = finallyUnitcodejiedao;
								}
								if(!finallyUnitcodeshequ.equals("")){
									unitcode = finallyUnitcodeshequ;
								}
								if(!finallyUnitcodexiaoqu.equals("")){
									xiaoqu = finallyUnitcodexiaoqu;
								}
								if(!finallyUnitcodelou.equals("")){
									lou = finallyUnitcodelou;
								}
								if(!finallyUnitcodedanyuan.equals("")){
									louunit = finallyUnitcodedanyuan;
								}
								if(!finallyUnitcodeceng.equals("")){
									ceng = finallyUnitcodeceng;
								}
								if(!finallyUnitcodeceng.equals("")){
									huhao = finallyUnitcodehuhao;
								}							
								
								
								//TODO: 执行检索异步线程
								new setTextViewValueTask().execute();
								dialog.dismiss();
							}
						});
				builder.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
							}
						});
				Dialog dialog = builder.create();
				// 设置不能被[返回键]关闭掉
				dialog.setCancelable(false);
				dialog.show();
			}
			
		});
	
	}

	/**
	 * 绑定listview
	 */
	private void initListView() {
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		mPullRefreshListView.setMode(Mode.BOTH);
		// 初始化数据
		initDatas();
		new InitDatas().execute();
		// 设置适配器
		simpleAdapter = new SimpleAdapter(PopulationListViewActivity.this,
				listItemArrayList, R.layout.listview_style_population,
				new String[] { "ItemID_population", "ItemName_population",
						"ItemSex_population", "ItemAddress_population" },
				new int[] { R.id.ItemID_population, R.id.ItemName_population,
						R.id.ItemSex_population, R.id.ItemAddress_population });
		mPullRefreshListView.setAdapter(simpleAdapter);
		mPullRefreshListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				TextView idTextView = (TextView) view
						.findViewById(R.id.ItemID_population);
				String populationID = idTextView.getText().toString();
				// 跳转到详细页面
				skipDetailActivity(populationID);
			}
		});

		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener2<ListView>() {
					@Override
					public void onPullDownToRefresh(
							PullToRefreshBase<ListView> refreshView) {
						Log.e("TAG", "onPullDownToRefresh");
						// 这里写下拉刷新的任务 page初始化为1
						page = 1;
						new GetDataTask().execute();
					}

					@Override
					public void onPullUpToRefresh(
							PullToRefreshBase<ListView> refreshView) {
						Log.e("TAG", "onPullUpToRefresh");
						// 这里写上拉加载更多的任务 page++
						page++;
						new GetDataTask().execute();
					}
				});
	}

	/**
	 * 跳转到mainmenu页面
	 */
	private void skipMainMenuActivity() {
		Intent mainmenuIntent = new Intent();
		mainmenuIntent
				.setClass(PopulationListViewActivity.this, Mainmenu.class);
		// 如果前面的activity 没有主动finish 推到栈顶，不然画面会刷一下
		// mainmenuIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PopulationListViewActivity.this.startActivity(mainmenuIntent);
		PopulationListViewActivity.this.finish();
	}

	/**
	 * 跳转到详细页面，并传入相应参数
	 */
	private void skipDetailActivity(String id) {
		Intent detailIntent = new Intent();
		detailIntent.putExtra("id", id);
		detailIntent.setClass(PopulationListViewActivity.this,
				PopulationDetailActivity.class);
		PopulationListViewActivity.this.startActivity(detailIntent);
		PopulationListViewActivity.this.finish();
	}

	/**
	 * 初始化控件值，必须有，防止ui阻塞
	 */
	private void initDatas() {
		// TextView 更新
		textViewUpdate();
		// listview
		listItemArrayList = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ItemID_population", "");
		map.put("ItemName_population", "");
		map.put("ItemSex_population", "");
		map.put("ItemAddress_population", "");
		listItemArrayList.add(map);
	}

	/**
	 * 更新页码等控件显示
	 */
	private void textViewUpdate() {
		totalPageTextView.setText("总页数:" + String.valueOf(totalPage));
		pageTextView.setText("当前页码:" + String.valueOf(page));
		recordsTextView.setText("总条数:" + String.valueOf(records));
	}

	/**
	 * 上拉下拉更新线程
	 * 
	 * @author Administrator
	 * 
	 */
	private class GetDataTask extends
			AsyncTask<Void, Void, ArrayList<HashMap<String, Object>>> {

		@Override
		protected ArrayList<HashMap<String, Object>> doInBackground(
				Void... params) {
			String resultString = getEventList();
			ArrayList<HashMap<String, Object>> finallyResult = jsonFormat(resultString);
			return finallyResult;
		}

		protected void onPostExecute(
				ArrayList<HashMap<String, Object>> finallyResultArrayList) {
			if (finallyResultArrayList.size() > 0) {
				if (page == 1) {
					listItemArrayList.clear();
				}
				listItemArrayList.addAll(finallyResultArrayList);
			}
			if (page > totalPage) {
				// 提示最后一页，page设置为totalpage
				page = totalPage;
				Toast.makeText(PopulationListViewActivity.this, "已经是最后一页",
						Toast.LENGTH_LONG).show();
			}
			// TextView 更新
			textViewUpdate();

			simpleAdapter.notifyDataSetChanged();
			// Call onRefreshComplete when the list has been refreshed.
			mPullRefreshListView.onRefreshComplete();
		}
	}

	/**
	 * 数据初始化
	 * 
	 * @author Administrator
	 * 
	 */
	private class InitDatas extends
			AsyncTask<Void, Void, ArrayList<HashMap<String, Object>>> {

		@Override
		protected void onPreExecute() {
			Log.e(null, "数据加载中");
			listItemArrayList.clear();
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemTitle", "数据加载中,请稍等。");
			listItemArrayList.add(map);
		}

		@Override
		protected ArrayList<HashMap<String, Object>> doInBackground(
				Void... params) {
			String resultString = getEventList();
			ArrayList<HashMap<String, Object>> finallyResult = jsonFormat(resultString);
			return finallyResult;
		}

		protected void onPostExecute(
				ArrayList<HashMap<String, Object>> finallyResultArrayList) {
			// TextView 更新
			textViewUpdate();
			listItemArrayList.clear();
			listItemArrayList.addAll(finallyResultArrayList);
			simpleAdapter.notifyDataSetChanged();
			// Call onRefreshComplete when the list has been refreshed.
			mPullRefreshListView.onRefreshComplete();
		}
	}

	/**
	 * 连接webservice，得到字符串
	 * 
	 * @return
	 */
	private String getEventList() {
		String resultString = "";
		Log.e(null, Thread.currentThread().getName() + "人口查询列表线程");
		String methodName = "get_PopulationQuery";
		String soapAcion = "http://tempuri.org/get_PopulationQuery";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);

		// todo 1112
		soapObject.addProperty("unitcode", unitcode);
		soapObject.addProperty("rows", rows);
		soapObject.addProperty("page", page);
		soapObject.addProperty("xiaoqu", xiaoqu);
		soapObject.addProperty("lou", lou);
		soapObject.addProperty("louunit", louunit);
		soapObject.addProperty("ceng", ceng);
		soapObject.addProperty("huhao", huhao);
		soapObject.addProperty("name", name);
		soapObject.addProperty("idnumber", idNumber);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAcion, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultString;
	}

	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private ArrayList<HashMap<String, Object>> jsonFormat(String resultString) {
		ArrayList<HashMap<String, Object>> tempArrayList = new ArrayList<HashMap<String, Object>>();
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			int len = jsonArray.length();
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				if(len != 1)
				{
				JSONObject totalJsonObject = jsonArray.optJSONObject(1);
				totalPage = Integer.valueOf(totalJsonObject.optString("total"))
						.intValue();
				records = Integer.valueOf(totalJsonObject.optString("records"))
						.intValue();
				}
				else {
					totalPage = 0;
					records = 0;
				}
				// 根据实际从第2(3)组开始取得有关数据
				for (int i = 2; i < len; i++) {
					jsonObject = jsonArray.optJSONObject(i);
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("ItemID_population", jsonObject.optString("personID"));
					map.put("ItemName_population", jsonObject.optString("name"));
					map.put("ItemSex_population", jsonObject.optString("gender"));
					map.put("ItemAddress_population",
							jsonObject.optString("address"));
					tempArrayList.add(map);
				}
			} else {
				// 没有数据，如何显示？已经到最后了
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tempArrayList;
	}

	/**
	 * 重写keydown事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			skipMainMenuActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 填充县下拉数据
	 */
	private class setxianSpinnerTask extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) shiSpinner.getSelectedItem()).getValue();
			String resultString = getSpinnerData("xian");
			String finallyResultString = jsonFormatSpinner(resultString, "xian");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = xianSpinnerArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass xianSpinnerClass = xianSpinnerArrayList.get(i);
					if (xianSpinnerClass.getValue().equals(xianUnitcode)) {
						xianSpinner.setSelection(i, true);
						xianSpinner.setEnabled(false);
						break;
					}
				}
				xianSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充街道下拉数据
	 */
	private class setjiedaoSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) xianSpinner.getSelectedItem())
					.getValue();
			String resultString = getSpinnerData("jiedao");
			String finallyResultString = jsonFormatSpinner(resultString,
					"jiedao");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = jiedaoSpinnerArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass jiedaoSpinnerClass = jiedaoSpinnerArrayList
							.get(i);
					if (jiedaoSpinnerClass.getValue().equals(jiedaoUnitcode)) {
						jiedaoSpinner.setSelection(i, true);
						jiedaoSpinner.setEnabled(false);
					}
				}
				jiedaoSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充社区下拉数据
	 */
	private class setshequSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) jiedaoSpinner.getSelectedItem())
					.getValue();
			String resultString = getSpinnerData("shequ");
			String finallyResultString = jsonFormatSpinner(resultString,
					"shequ");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 初始化同时删除其他选项，只能显示自己和下级的数据。
				int len = shequSpinnerArrayList.size();
				for (int i = 0; i < len; i++) {
					SpinnerClass shequSpinnerClass = shequSpinnerArrayList
							.get(i);
					if (shequSpinnerClass.getValue().equals(shequUnitcode)) {
						shequSpinner.setSelection(i, true);
						shequSpinner.setEnabled(false);
					}
				}
				shequSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充小区下拉数据
	 */
	private class setxiaoquSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			unitcode = ((SpinnerClass) shequSpinner.getSelectedItem())
					.getValue();
			String resultString = getSpinnerData("xiaoqu");
			String finallyResultString = jsonFormatSpinner(resultString,
					"xiaoqu");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				xiaoquSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 填充楼下拉数据
	 */
	private class setlouSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			String resultString = getSpinnerData("lou");
			String finallyResultString = jsonFormatSpinner(resultString, "lou");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				louSpinnerAdapter.notifyDataSetChanged();
			}
		}

	}

	/**
	 * 填充单元下拉数据
	 */
	private class setunitSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			String resultString = getSpinnerData("danyuan");
			String finallyResultString = jsonFormatSpinner(resultString, "danyuan");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				danyuanSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}
	
	/**
	 * 填充层下拉数据
	 */
	private class setcengSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			String resultString = getSpinnerData("ceng");
			String finallyResultString = jsonFormatSpinner(resultString, "ceng");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				cengSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}
	
	/**
	 * 填充户号下拉数据
	 */
	private class sethuhaoSpinnerTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			String resultString = getSpinnerData("huhao");
			String finallyResultString = jsonFormatSpinner(resultString, "huhao");
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				huhaoSpinnerAdapter.notifyDataSetChanged();
			}
		}
	}

	/**
	 * 得到详细信息 ,
	 * 
	 * @return
	 */
	private String getSpinnerData(String jibie) {
		// TODO Auto-generated method stub
		String resultString = "";
		if(jibie.equals("huhao"))
		{
			methodName = "get_HuhaoByCeng";
			soapAcion = "http://tempuri.org/get_HuhaoByCeng";
		} else if(jibie.equals("ceng"))
		{
			methodName = "get_CengByLouunit";
			soapAcion = "http://tempuri.org/get_CengByLouunit";
		} else if(jibie.equals("danyuan"))
		{
			methodName = "get_LouunitByLou";
			soapAcion = "http://tempuri.org/get_LouunitByLou";
		} else if (jibie.equals("lou")) {
			methodName = "get_LouByXiaoqu";
			soapAcion = "http://tempuri.org/get_LouByXiaoqu";
		} else if (jibie.equals("xiaoqu")) {
			methodName = "get_XiaoquByShequ";
			soapAcion = "http://tempuri.org/get_XiaoquByShequ";
		} else {
			methodName = "GetUnit";
			soapAcion = "http://tempuri.org/GetUnit";
		}

		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
		// 操作人员所属区域编码
		if (!jibie.equals("xiaoqu") && !jibie.equals("lou") && !jibie.equals("danyuan")) {
			soapObject.addProperty("type", "0");
		}
		// 这个webservice写成了unitCode，日后改为unticode
		if (jibie.equals("xiaoqu")) {
			soapObject.addProperty("unitCode", unitcode);
		} else {
			soapObject.addProperty("unitcode", unitcode);
		}
		if (jibie.equals("lou")) {
			soapObject
					.addProperty("xiaoqu", ((SpinnerClass) xiaoquSpinner
							.getSelectedItem()).getValue());
		}
		if (jibie.equals("danyuan") || jibie.equals("ceng") || jibie.equals("huhao")) {
			soapObject
			.addProperty("xiaoqu", ((SpinnerClass) xiaoquSpinner
					.getSelectedItem()).getValue());
			soapObject
					.addProperty("lou", ((SpinnerClass) louSpinner
							.getSelectedItem()).getValue());
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			// 必须要判断下 ，因为让TableRow 显示，就相当于控件初始化一次？默认选中第一个
			if (!unitcode.equals("")) {
				transportSE.call(soapAcion, envelope);
				if (envelope.getResponse() != null) {
					SoapObject object = (SoapObject) envelope.bodyIn;
					resultString = object.getProperty(0).toString();
					Log.e("webservice取得" + jibie, resultString);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("IOException", e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("XmlPullParserException", e.getMessage());
		}
		return resultString;
	}

	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private String jsonFormatSpinner(String resultString, String jibie) {
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			int len = jsonArray.length();
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				if (jibie.equals("xian")) {
					xianSpinnerArrayList.clear();
					xianSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						xianSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("unitname"), jsonObject
								.optString("unitcode")));
					}
				} else if (jibie.equals("jiedao")) {
					jiedaoSpinnerArrayList.clear();
					jiedaoSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						jiedaoSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("unitname"), jsonObject
								.optString("unitcode")));
					}
				} else if (jibie.equals("shequ")) {
					shequSpinnerArrayList.clear();
					shequSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						shequSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("unitname"), jsonObject
								.optString("unitcode")));
					}
				} else if (jibie.equals("xiaoqu")) {
					xiaoquSpinnerArrayList.clear();
					xiaoquSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						// 小区返回的unitcode，name, 这里都用name
						xiaoquSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("name"), jsonObject
								.optString("name")));
					}
				} else if (jibie.equals("lou")) {
					louSpinnerArrayList.clear();
					louSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						louSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("Lou"), jsonObject
								.optString("Lou")));
					}
				} else if(jibie.equals("danyuan"))
				{
					danyuanSpinnerArrayList.clear();
					danyuanSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						danyuanSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("Loununit"), jsonObject
								.optString("Loununit")));
					}
				} else if(jibie.equals("ceng"))
				{
					cengSpinnerArrayList.clear();
					cengSpinnerArrayList.add(new SpinnerClass("请选择", ""));
					for (int i = 1; i < len; i++) {
						// 根据实际从第2组开始取得有关数据
						jsonObject = jsonArray.optJSONObject(i);
						cengSpinnerArrayList.add(new SpinnerClass(jsonObject
								.optString("Ceng"), jsonObject
								.optString("Ceng")));
					}
				}
				 else if(jibie.equals("huhao"))
					{
						huhaoSpinnerArrayList.clear();
						huhaoSpinnerArrayList.add(new SpinnerClass("请选择", ""));
						for (int i = 1; i < len; i++) {
							// 根据实际从第2组开始取得有关数据
							jsonObject = jsonArray.optJSONObject(i);
							huhaoSpinnerArrayList.add(new SpinnerClass(jsonObject
									.optString("Huhao"), jsonObject
									.optString("Huhao")));
						}
					}
				return "success";
			} else {
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}

	/**
	 * 设置textView的值
	 */
	private class setTextViewValueTask extends AsyncTask<Void, Void, String> {

		protected void onPreExecute() {			
			//setTextViewValue();
			initListView();
			Toast.makeText(PopulationListViewActivity.this, "数据加载中...", Toast.LENGTH_LONG).show();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getEventList();
			
			String finallyResultString = jsonFormat(resultString).toString();
			return finallyResultString;
		}
		

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 设置TextView的
				Toast.makeText(PopulationListViewActivity.this, "数据加载完毕！", Toast.LENGTH_LONG).show();
				initListView();
			}
		}

	}

	/**
	 * 初始化下拉列表用的几个unitcode
	 */
	private void getMyUnitcodeAbout() {
		new getMyUnitcodeAboutTask().execute();
	}
	
	/**
	 * 
	 */
	private class getMyUnitcodeAboutTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String resultString = getMyUnitcodeAboutData();
			String finallyResultString = jsonFormatMyUnitcodeAbout(resultString);
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				Log.e("几个unitcode", "成功得到");
			}
		}

	}
	
	/**
	 * 得到几个unitcode 相关数据
	 * 
	 * @return
	 */
	private String getMyUnitcodeAboutData() {
		// TODO Auto-generated method stub
		String resultString = "";
		methodName = "GetUnit";
		soapAcion = "http://tempuri.org/GetUnit";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
		// 操作人员所属区域编码
		soapObject.addProperty("type", "2");
		soapObject.addProperty("unitcode", unitcode);// 使用MyUnitcode得到上级unitcode
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAcion, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("IOException", e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("XmlPullParserException", e.getMessage());
		}
		return resultString;
	}

	
	private String jsonFormatMyUnitcodeAbout(String resultString) {
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			int len = jsonArray.length();
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				// 根据实际从第2组开始取得有关数据 最多一共5组
				if (len >= 5) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(2);
					xianUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(3);
					jiedaoUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(4);
					shequUnitcode = jsonObject.optString("unitcode");
				} else if (len >= 4) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(2);
					xianUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(3);
					jiedaoUnitcode = jsonObject.optString("unitcode");
				} else if (len >= 3) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
					jsonObject = jsonArray.optJSONObject(2);
					xianUnitcode = jsonObject.optString("unitcode");
				} else if (len >= 2) {
					jsonObject = jsonArray.optJSONObject(1);
					shiUnitcode = jsonObject.optString("unitcode");
				}
				return "success";
			} else {
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}

}
